package cn.devices.service.impl;

import cn.devices.TbDmDto;
import cn.devices.entity.TbDm;
import cn.devices.entity.TbType;
import cn.devices.mapper.TbDmMapper;
import cn.devices.model.TbDmModel;
import cn.devices.service.TbDmService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author 秋水浮萍
 * @since 2021-02-07
 */
@Service
public class TbDmServiceImpl  implements TbDmService {

    @Autowired
    private TbDmMapper tbDmMapper;

    @Override
    public int tbDmAdd(TbDm tbDm) {
        return tbDmMapper.tbDmAdd(tbDm);
    }

    @Override
    public int tbDmDeleteFalse(String depCode) {
        return tbDmMapper.tbDmDeleteFalse(depCode);
    }

    @Override
    public int tbDmDeleteTrue(String depCode) {
        return tbDmMapper.tbDmDeleteTrue(depCode);
    }

    @Override
    public int tbDmUpdate(TbDmDto dto) {
        return tbDmMapper.tbDmUpdate(dto);
    }

    @Override
    public int countDmName(String depCodeOriginal, String depName) {
        return tbDmMapper.countDmName(depCodeOriginal, depName);
    }

    @Override
    public int count(String depCode, String depName) {
        return tbDmMapper.count(depCode,depName);
    }

    @Override
    public PageInfo<TbDm> tbDmList(int pageIndex, int pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        List<TbDm> list=tbDmMapper.tbDmList();
        List<TbDmModel> tbDmModel=tbDmMapper.tbDmModel();
        for (TbDm t:list
             ) {
            if (t.getTbAuthDmList()==null) t.setTbAuthDmList(new ArrayList<>());
            for (TbDmModel ts : tbDmModel){
                if (t.getDepCode().equals(ts.getDepCode())){
                    TbType tbType=new TbType();
                    tbType.setTypeId(ts.getTypeId());
                    tbType.setTypeName(ts.getTypeName());
                    tbType.setTypeLevel(ts.getTypeLevel());
                    t.getTbAuthDmList().add(tbType);
                }
            }

        }
        return new PageInfo<>(list);
    }

    @Override
    public List<TbDmModel> list() {
        return tbDmMapper.list();
    }


    @Override
    public TbDm tbDmSelect(String depCode) {
        return tbDmMapper.tbDmSelect(depCode);
    }

    @Override
    public int d() {
        return 0;
    }
}
